/*
 * @features: 相机控制
 * @description: 说明
 * @Date: 2020-10-14 21:09:47
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2020-10-27 22:37:51
 * @LastEditors: judu233
 */

import z from "../../Manager/Z";

const { ccclass, property, disallowMultiple } = cc._decorator;

/**  
    * @Title : 相机控制
    * @Description : 类说明
 **/
@ccclass
@disallowMultiple
export default class CameraContrl extends cc.Component {

    @property(cc.Node)
    public centerNode: cc.Node = null;

    /**
     * 开始时是视图移动到第一个按钮位置
     * @param targetPos 当前按钮位置
     */
    public initCamera(targetPos: cc.Vec3) {
        this.node.scale = 0.4;
        //移动到开始按钮
        this.updateCamera(targetPos, this.node.position,
            cc.tween(this.node).to(0.6, { scale: 1 }, cc.easeSineIn()));
    }

    /**
     * 点击结束按钮时 视图移动到地图中心位置
     * @param centerNode 当前按钮位置
     */
    public outCamera(centerNode: cc.Node) {
        //地图中间按钮
        let centerWoredPos = centerNode.convertToWorldSpaceAR(cc.v3(0, 0, 0));
        //屏幕中间位置
        let screenPos = this.centerNode.convertToWorldSpaceAR(cc.v3(0, 0, 0));
        let movePos = this.node.position.add(screenPos.sub(centerWoredPos));
        cc.tween(this.node)
            .parallel(
                cc.tween().to(0.4, { position: movePos }, cc.easeSineIn()),
                cc.tween(this.node).to(0.6, { scale: 0.4 }, cc.easeSineIn())
            ).start();
    }

    /**更新摄像机的位置 */
    public updateCamera(targetPos: cc.Vec3, nowPos: cc.Vec3, tween?: cc.Tween) {
        //获取要移动的向量
        let moveVector = targetPos.clone().sub(nowPos.clone()).neg();
        let nodePos = this.node.position.clone();
        let otherTween = is.undefined(tween) ? cc.tween().delay(0.1) : tween;

        cc.tween(this.node)
            .parallel(
                cc.tween().to(0.4, { position: nodePos.add(moveVector) }, cc.easeCubicActionInOut()),
                otherTween
            )
            .start();
    }
}
